if lib and not lib.isConnected then


function lib:isConnected()
	--message("lib:isConnected " .. isConnected())
	local connected = {
		{isConnected(), 0},
		{self:GetUser(), ""},
		{self:GetClientCode(), ""},
		{int(getInfoParam("MESSAGESRECV")), 3000},
		{getNumberOf("securities"), 3000},
		{self:GetAssets(), 0},
	}	
	
	for i, v in ipairs(connected) do
		if not v[1] or v[1] <= v[2] then
			--message("isConnected " .. i .. " " .. self:toline(v))
			return false, i
		end
	end

	--return lib:SubscribeLevel2orders()
	
	return true --self:isTradeTime()
end


function lib:IsTest()
	local id = self:GetUser()
	
	return id and id ~= ""
			and lib.global
			and lib.global.test_accounts 
			and lib.global.test_accounts[id]
end


function lib:GetUser()
	return string.match(getInfoParam("USER"), "([A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]+)")
end


function lib:GetUID()
	return getInfoParam("USERID")
end


function lib:GetTrdAcc()
	local ml = self:GetMoneyLimitInfo()
	--message(lib:toline(ml))
	--local ml = self:GetTable("futures_client_limits")
	--if ml and #ml > 0 then
	--	return ml[1].trdaccid
	--end
	if ml then return ml.trdaccid end
end


function lib:GetClientCode()
	--message("GetClientCode 1 " .. self:toline(self:GetTable("client_codes")))
	--local ml = self:GetMoneyLimitInfo()
	--if ml then return ml.client_code end
	--local cc = self:GetTable("trade_accounts")
	--self:SaveFile(self.path.log .. "trade_accounts.txt", self:tostring(cc))
	local cc = self:GetTable("money_limits")
	--message("GetClientCode 2 " .. self:toline(cc))
	if #cc > 0 then
		return cc[1].client_code
	end
	--self:SaveFile(self.path.log .. "money_limits.txt", self:tostring(cc))
	--message("GetClientCode 3 " .. self:toline(cc))
	cc = self:GetTable("depo_limits")
	--message("GetClientCode 3 " .. self:toline(cc))
	--self:SaveFile(self.path.log .. "depo_limits.txt", self:tostring(cc))
	--message("GetClientCode 2 " .. self:toline(cc))
	--local cc = self:GetTable("client_codes")
	--self:SaveFile(self.path.log .. "client_codes.txt", self:tostring(cc))
	--message("GetClientCode 4 " .. self:toline(cc))
	if #cc > 0 then
		return cc[1].client_code
	end
	local cc = self:GetTable("client_codes")
	--message("GetClientCode 2 " .. self:toline(cc))
	if #cc > 0 then
		return cc[#cc]
	end
end


function lib:GetFirmID()
	local ml = self:GetMoneyLimitInfo()
	--if ml then return ml.firmid end
	--local ml = self:GetTable("money_limits")
	--if ml and #ml > 0 then
	--	return ml[1].firmid
	--end
	if ml then return ml.firmid end
end


function lib:GetAssets()
	if not self:GetClientCode() then return end
	if not self:GetUser() then return end
	
	local pf = getPortfolioInfoEx(self:GetFirmID(), self:GetClientCode(), 2)
	local fl = self:GetTable("futures_client_limits", "limit_type")[0]
	--local fl = getFuturesLimit(self:GetFirmID(), self:GetAccount("SPBFUT"), 2, "")
	--message("lib:GetAssets " .. lib:toline(pf))
	--self:SaveFile(self.path.log .. "GetAssets." .. self:GetUser() .. ".txt", self:tostring(pf))
	--self:SaveFile(self.path.log .. "futures_client_limits." .. self:GetUser() .. ".txt", self:tostring(self:GetTable("futures_client_limits")))
	
	local all_assets = 0
	local in_all_assets = 0
	local cbplimit = 0
	local varmargin = 0
	local accruedint = 0
	local ts_comission = 0
	
	if pf and pf.all_assets and pf.in_all_assets
	then
		all_assets = pf.all_assets
		in_all_assets = pf.in_all_assets
	end
	
	if fl and fl.cbplimit and fl.varmargin and fl.accruedint and fl.ts_comission
	then
		cbplimit = fl.cbplimit
		varmargin = fl.varmargin
		accruedint = fl.accruedint
		ts_comission = fl.ts_comission
	end
	
	local a = 0
	--math.min(
		--tonumber(pf.portfolio_value),
		--tonumber(pf.assets),
		--tonumber(pf.all_assets)
		--tonumber(pf.fut_total_asset)
	--)
	--if pf.all_assets then
		a = a + all_assets
	--end
	--if fl.cbplimit and fl.varmargin and fl.accruedint and fl.ts_comission then
		a = a + cbplimit + varmargin + accruedint + ts_comission
	--end
	
	local ina = in_all_assets + cbplimit
	
	a = lib:LoadIni(lib.path.init .. self:GetUser() .. ".ini", nil, {
		assets = a,
		in_assets = ina,
		hidden_assets = 0,
	})
	
	--message(lib:toline(a))
	
	ina = a.in_assets
	a = a.assets - a.hidden_assets

	if a > 500 and ina > 0
		and math.abs(a / ina) < 2
		and math.abs(a / ina) > 0.5
	then
		return a
	end
	--message("GetAssets error", 3)
end


function lib:GetPL()
	local pf = getPortfolioInfoEx(self:GetFirmID(), self:GetClientCode(), 2)
	local fl = self:GetTable("futures_client_limits", "limit_type")[0]
	
	local profit_loss = 0
	local in_all_assets = 0
	local cbplimit = 0
	local varmargin = 0
	local accruedint = 0
	local ts_comission = 0
	
	if pf and pf.profit_loss and pf.in_all_assets
	then
		profit_loss = pf.profit_loss
		in_all_assets = pf.in_all_assets
	end
	
	if fl and fl.cbplimit and fl.varmargin and fl.accruedint and fl.ts_comission
	then
		cbplimit = fl.cbplimit
		varmargin = fl.varmargin
		accruedint = fl.accruedint
		ts_comission = fl.ts_comission
	end
	
	local a = 0
	a = a + profit_loss
	a = a + varmargin + accruedint + ts_comission
	
	local ina = in_all_assets + cbplimit
	
	if ina > 0 then
		return a, a / ina * 100
	end
	
	return 0, 0
end


function lib:GetMoneyLimitInfo()
	local limit_kind = -1
	local m = {}
	
	--self:SaveFile(self.path.log .. "money_limits.txt", self:tostring(self:GetTable("money_limits", "limit_kind")))
	for k, data in pairs(self:GetTable("money_limits")) do
		if data.currcode == "SUR" and limit_kind < k then
			limit_kind = k
			m = data
			--message(self:toline(data))
		end
	end
	
	--self:SaveFile(self.path.log .. "futures_client_limits.txt", self:tostring(self:GetTable("futures_client_limits", "limit_type")))
	if m then
		local fl = self:GetTable("futures_client_limits")[1]
		if fl then
			m.trdaccid = fl.trdaccid
			if fl.cbplimit and fl.cbplimit > 0 then
				m.currentbal = (m.currentbal or 0) + fl.cbplimit
			end
		end
	end
	
	return m
end


function lib:GetMoney()
	local m = self:GetMoneyLimitInfo()
	--message(self:toline(m))
	if m then
		return m.currentbal, m.locked or 0
	end
	return 0
end


function lib:GetAccount(class_code)
	if not self.trade_accounts then self.trade_accounts = {} end
	local account = self.trade_accounts[class_code]
	if not account then
		for k, data in pairs(self:GetTable("trade_accounts", "trdaccid")) do
			if string.find(data.class_codes, class_code) then 
				self.trade_accounts[class_code] = k
				return k
			end
		end
	end
	return account
end


function lib:wasTrade(sec_code)
	if not self.sec_filters then self.sec_filters = {} end
	local fn = self.sec_filters[sec_code]
	if not fn and sec_code then
		fn = [[
			local sec_code = ...
			if sec_code ~= "%s" then return false end
		]]
		local err
		fn, err = load(string.format(fn, sec_code))
		if fn then
			self.sec_filters[sec_code] = fn
		end
	end
	if fn then
		local t1 = SearchItems("all_trades", 0, getNumberOf("all_trades")-1, fn, "sec_code")
		if t1 and #t1 > 0 then return true end
	end
end


end -- of file
